<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{commons/common::head(~{::title})}">
    <title>博客详情</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<!--导航栏 menu-->
<div th:replace="~{commons/common::menu}"></div>

<!--中间部分-->
<div id="waypoint" class="m-container m-padded-tb-big animated fadeIn">
    <div class="ui container">
        <!--小头像-->
        <div class="ui top attached segment">

            <div class="ui horizontal link list">
                <div class="item">
                    <img src="https://picsum.photos/id/1/100/100" alt="" class="ui avatar image"
                         th:src="@{${detailedBlog.avatar}}">
                    <div class="content">
                        <a class="header" th:text="${detailedBlog.nickname}">孙忠杰</a>
                    </div>
                </div>
                <div class="item">
                    <i class="calendar icon"></i> <span
                        th:text="${#dates.format(detailedBlog.createTime, 'yyyy-MM-dd')}">2020-03-04</span>
                </div>
                <div class="item">
                    <i class="eye icon"></i> <span th:text="${detailedBlog.views}">6666</span> 次浏览
                </div>
            </div>
        </div>
        <!--博客图片-->
        <div class="ui attached segment">
            <div class="ui basic segment">
                <img src="https://picsum.photos/id/1/800/450" alt="" class="ui fluid rounded image"
                     th:src="@{${detailedBlog.firstPicture}}">
            </div>

        </div>
        <!--博客内容-->
        <div class="ui attached padded segment">

            <!--个性标签-->
            <div class="ui basic segment right aligned">
                <div class="ui blue basic label" th:text="${detailedBlog.flag}">随笔</div>
            </div>

            <!--文章标题-->
            <h2 class="ui header center aligned" th:text="${detailedBlog.title}">Java基础入门</h2>

            <!--文章内容-->
            <div id="content" class="typo typo-selection js-toc-content m-padded-lr-responsive m-padded-tb-large"
                 th:utext="${detailedBlog.content}"></div>

            <!--分类-->
            <div class="m-padded-lr-responsive">
                <div class="ui basic left pointing blue label" th:text="${tag.name}"
                     th:each="tag : ${detailedBlog.tags}">Java基础
                </div>
            </div>


            <!--打赏按钮-->
            <div class="ui basic center aligned segment" th:if="${detailedBlog.isAppreciation()}">
                <button id="payButton" class="ui blue basic circular button">打赏</button>
            </div>
            <!--支付图片弹出-->
            <div class="ui payActive flowing popup transition hidden">
                <div class="ui orange basic label">
                    <div class="ui images">
                        <div class="image">
                            <img class="ui image" th:src="@{/images/alipay.jpg}" style="width: 110px;">
                            <div>支付宝 visible animating scale out</div>
                        </div>

                        <div class="image">
                            <img class="ui image" th:src="@{/images/wechatpay.png}" style="width: 110px;">
                            <div>微信</div>
                        </div>
                    </div>
                </div>

            </div>

        </div>
        <!--作者信息-->
        <div class="ui attached positive message" th:if="${detailedBlog.isShareStatement()}">
            <div class="ui grid">
                <!--作者信息-->
                <div class="ui middle aligned eleven wide column">
                    <ui class="list">
                        <li>作者：<span th:text="${detailedBlog.nickname}">author</span></li>
                        <li>发表时间：<span
                                th:text="${#dates.format(detailedBlog.createTime, 'yyyy-MM-dd')}">2020-03-04</span></li>
                        <li>版权声明：笔记撰写不易，转发请标记出处。</li>
                        <li><span>不吃学习的苦，就吃生活的苦！</span></li>
                    </ui>
                </div>
                <!--二维码-->
                <div class="five wide column">
                    <img th:src="@{/images/wechat.jpg}" alt=""
                         class="ui rounded bordered right aligned floated image" style="width: 110px;">
                </div>
            </div>
        </div>

        <!-- 留言信息-->
        <div class="ui bottom attached segment" th:if="${detailedBlog.isCommentabled()}">
            <!--展示留言区域-->
            <div id="comment-container" class="ui blue segment">
                <div th:fragment="commentList">
                    <div class="ui threaded comments" style="max-width: 100% !important;">
                        <h3 class="ui dividing header">评论</h3>
                        <div class="comment" th:each="comment : ${comments}">
                            <a class="avatar">
                                <img th:src="${comment.avatar}">
                            </a>
                            <div class="content">
                                <a class="author">
                                    <span th:if="${!comment.adminComment}" th:text="${comment.nickname}">张三</span>
                                    <span th:if="${comment.adminComment}" th:text="${comment.nickname}">张三</span>
                                    <div th:if="${comment.adminComment}"
                                         class="ui mini blue basic left pointing label m-padded-mini">博主
                                    </div>
                                </a>
                                <div class="metadata">
                                    <span class="date"
                                          th:text="${#dates.format(comment.createTime, 'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                                </div>
                                <div class="text" th:text="${comment.content}">
                                    good Blog！
                                </div>
                                <div class="actions">
                                    <a class="reply" data-commentid="1" data-commentnickname="Rain"
                                       th:attr="data-commentid=${comment.id}, data-commentnickname=${comment.nickname} "
                                       onclick="reply(this)">回复</a>
                                </div>
                            </div>

                            <div class="comments" th:if="${#arrays.length(comment.replyComments) > 0}"
                                 style="max-width: 100% !important;">
                                <div class="comment" th:each="reply : ${comment.replyComments}">
                                    <a class="avatar">
                                        <img th:src="${reply.avatar}">
                                    </a>
                                    <div class="content">
                                        <a class="author">
                                            <span th:if="${!reply.adminComment}" th:text="${reply.nickname}">张三</span>
                                            <span th:if="${reply.adminComment}" th:text="${reply.nickname}">张三</span>
                                            <div th:if="${reply.adminComment}"
                                                 class="ui mini blue basic left pointing label m-padded-mini">博主
                                            </div>
                                            &nbsp;<span th:if="${reply.parentComment != null}"
                                                        th:text="|@ ${reply.parentComment.nickname} |"
                                                        class="m-blue-color">李四</span>
                                        </a>

                                        <div class="metadata">
                                    <span class="date"
                                          th:text="${#dates.format(reply.createTime, 'yyyy-MM-dd HH:mm')}">Today at 5:42PM</span>
                                        </div>
                                        <div class="text" th:text="${reply.content}">
                                            good Blog！
                                        </div>
                                        <div class="actions">
                                            <a class="reply" data-commentid="1" data-commentnickname="Rain"
                                               th:attr="data-commentid=${reply.id}, data-commentnickname=${reply.nickname} "
                                               onclick="reply(this)">回复</a>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!--留言form表单-->
            <form class="ui form" id="comment-form" method="post">
                <input type="hidden" name="blogId" th:value="${detailedBlog.id}" id="blogId">
                <input type="hidden" name="parentCommentId" value="-1">

                <!--留言文本区域-->
                <div class="field">
                    <textarea name="content" id="" cols="30" rows="10" placeholder="在此处留言......"></textarea>
                </div>
                <!--访客填写字段-->
                <div class="fields">
                    <!--昵称-->
                    <div class="field m-mobile-width m-margin-bottom-small">
                        <div class="ui left icon input">
                            <i class="user icon"></i>
                            <input type="text" name="nickname" placeholder="昵称"
                                   th:value="${session.loginUser != null} ? ${session.loginUser.nickname}">
                        </div>
                    </div>
                    <!--邮箱-->
                    <div class="field m-mobile-width m-margin-bottom-small">
                        <div class="ui left icon input">
                            <i class="mail icon"></i>
                            <input type="email" name="email" placeholder="邮箱"
                                   th:value="${session.loginUser != null } ? ${session.loginUser.email}">
                        </div>
                    </div>
                    <!--提交按钮-->
                    <div class="field m-mobile-width m-margin-bottom-small">
                        <button type="button" id="commentpost-btn" class="ui blue button m-mobile-width"><i
                                class="edit icon"></i>提交
                        </button>
                    </div>

                </div>
            </form>

        </div>

    </div>

</div>

<!--右下角的工具条-->
<div class="m-fixed m-right-bottom m-padded" id="toolbar" style="display: none;">
    <div class="ui vertical icon buttons">
        <button type="button" class="ui blue toc button">目录</button>
        <a href="#comment-container" class="ui blue button">评论</a>
        <button class="ui wechat icon button"><i class="weixin icon"></i></button>
        <div id="toTop-button" class="ui icon button"><i class="chevron up icon"></i></div>
    </div>

</div>

<div class="ui top-container popup flowing transition hidden" style="width: 250px;">
    <ol class="js-toc"></ol>
</div>

<div id="qrcode" class="ui wechat-qr flowing popup transition hidden" style="width: 130px !important;">
</div>


<!--底部 footer-->
<div th:replace="~{commons/common::footer}"></div>

<!-- 引用的 js-->
<th:block th:replace="~{commons/common::script}">
</th:block>


<script th:inline="javascript">
    // 在手机端点击汉堡按钮，重新显示隐藏的导航栏内容
    $('.menu.toggle').click(function () {
        $('.m-item').toggleClass('m-mobile-hidden');
    });


    // 二维码点击弹出
    $('#payButton').popup({
        popup: $('.payActive.popup'),
        on: 'click',
        position: 'bottom center'
    });

    // 目录初始化
    tocbot.init({
        // Where to render the table of contents.
        tocSelector: '.js-toc',
        // Where to grab the headings to build the table of contents.
        contentSelector: '.js-toc-content',
        // Which headings to grab inside of the contentSelector element.
        headingSelector: 'h1, h2, h3, h4',
        // For headings inside relative or absolute positioned containers within content.
        hasInnerContainers: true,
    });


    // 点击目录，弹出
    $('.toc.button').popup({
        popup: $('.top-container.popup'),
        on: 'click',
        position: 'left center'
    });


    // 搜索框弹出消息
    $('.m-popup input').popup({
        on: 'focus',
        position: 'left center'
    });

    // 弹出微信图片
    $('.wechat').popup({
        popup: $('.wechat-qr')
    });

    // 右下角文章二维码
    let serverURL = "114.116.244.214";
    let articleURL =  /*[[@{/article/} + ${detailedBlog.id}]]*/"";
    var qrcode = new QRCode("qrcode", {
        text: serverURL + articleURL,
        width: 110,
        height: 110,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H
    });

    // 评论校验
    $('#commentpost-btn').click(() => {
        let flag = $('.ui.form').form('validate form');
        if (flag) {
            console.log('校验成功');
            sendData();
        } else {
            console.log('校验失败');
        }
    });

    $(() => {
        let blogId = $('#blogId').val();
        $('#comment-container').load('/comments/' + blogId);
    });

    // /*[[@{/comments}]]*/
    // 发送评论请求
    function sendData() {
        $('#comment-container').load('/comments', {
            "parentCommentId": $("[name='parentCommentId']").val(),
            "blogId": $("[name='blogId']").val(),
            "nickname": $("[name='nickname']").val(),
            "email": $("[name='email']").val(),
            "content": $("[name='content']").val()
        }, function (responseTxt, statusTxt, xhr) {
            // 滚动到评论区
            //  $(window).scrollTo($('#comment-container'), 500);
            // 清除评论内容
            clearContent();
        });
    }

    // 清除评论内容
    function clearContent() {
        $("[name='content']").val('');
        $("[name='parentCommentId']").val(-1);
        $("[name='content']").attr("placeholder", "请输入评论信息...");
    }

    function reply(obj) {
        let commentId = $(obj).data('commentid');
        let commentNickname = $(obj).data('commentnickname');
        $("[name='content']").attr("placeholder", "@" + commentNickname).focus();
        $("[name='parentCommentId']").val(commentId);
        $(window).scrollTo($('#comment-container'), 500);
    }

    // 平滑滚动
    $('#toTop-button').click(function () {
        $(window).scrollTo(0, 500);
    });

    // 滚动侦测
    var waypoint = new Waypoint({
        element: document.getElementById('waypoint'),
        handler: function (direction) {
            if (direction == 'down') {
                $('#toolbar').show(100);
            } else {
                $('#toolbar').hide(500);
            }
        }
    });

    //评论表单验证
    $('.ui.form').form({
        fields: {
            title: {
                identifier: 'content',
                rules: [{
                    type: 'empty',
                    prompt: '请输入评论内容'
                }
                ]
            },
            content: {
                identifier: 'nickname',
                rules: [{
                    type: 'empty',
                    prompt: '请输入你的昵称'
                }]
            },
            type: {
                identifier: 'email',
                rules: [{
                    type: 'email',
                    prompt: '请填写正确的邮箱地址'
                }]
            }
        }
    });


</script>
</body>
</html>